// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Sphere.h"

Sphere::Sphere(double radius, int spacing, const QString &name) :
    Item(name)
{
    if (90 % spacing != 0) {
        qDebug() << "90/spacing must be an integer. Setting to 10.";
        spacing = 10;
    }
    if (radius <= 0.0) {
        qDebug() << "radius must be positive";
        radius = 1.0;
    }

    //START//
    double theta, phi, rTheta, rPhi, rPhi2;

    for( phi = -90; phi <= 90 - spacing; phi+=spacing){
        Face triangles(Face::TriangleStrip);

        rPhi = phi / 180.0 * M_PI;
        rPhi2 = (phi + spacing) / 180.0 * M_PI;

        for( theta = 0; theta <= 360; theta+=spacing){
            rTheta = theta / 180.0 * M_PI;

            Vector n0 = Matrix::rotationZ(rTheta) * Matrix::rotationY(rPhi) * Vector::ORTHO_X;
            Vector n1 = Matrix::rotationZ(rTheta) * Matrix::rotationY(rPhi2) * Vector::ORTHO_X;

            Vector v0 = n0 * radius;
            Vector v1 = n1 * radius;

            Vector co0 = Vector(theta/360, (phi + 90)/180, 0);
            Vector co1 = Vector(theta/360, (phi + 90 + spacing)/180, 0);;

            triangles.addPoint(v0, n0, co0);
            triangles.addPoint(v1, n1, co1);

        }
        addFace(triangles);
    }
    //END//
    /* STOP here */
}
